import store from '@/store';
import bigScreen from "./bigScreen"; // 导入 store 实例

// 定义 WebSocket 模块的初始状态
const state = {
  websocket: null, // 存储 WebSocket 实例
  reconnectAttempts: 0, // 记录重连次数
  maxReconnectAttempts: 3, // 最大重连次数
  isConnected: false, // 标识是否已经连接
  basic: [], // 存储实时基础状态
  issue_task: [], // 下发导航任务
  cancel_task: [], // 取消导航任务
  search_task: [], // 查询导航任务状态
  motion_control: [], // 运动控制
  power: [], // 电量信息
  videoBlob: null, // 存储接收的视频流数据
  rollCall: {},// 存储大屏左边的信息
  cruiseDate: {},
  onlineState: 1,
  nomoveTimes: 0,
  personRollCall: {},//显示三秒的点名信息
  firePersonError: {},//显示三秒的异常信息

  statistics:{}
};

// 定义用于修改状态的 mutations，通过这个地方判断是什么类型的信息，然后页面调用不同的信息
const mutations = {
  // 设置 WebSocket 实例
  setWebSocket(state, websocket) {
    state.websocket = websocket;
  },
  // 添加接收到的消息到消息列表
  addMessage(state, message) {
    // 判断是狗还是人脸
    // console.log(message)
    const { code, jsonObject } = message;
    // console.log("收到websocket信息：")
    // console.log(jsonObject)
    if (code === 0 && jsonObject) {
      const { type } = jsonObject;
      switch (type) {
        case 1002:
          state.basic.push(jsonObject);
          break;
        case 1003:
          state.issue_task.push(jsonObject);
          break;
        case 1004:
          state.cancel_task.push(jsonObject);
          break;
        case 1007:
          state.search_task.push(jsonObject);
          break;
        case 2:
          state.motion_control.push(jsonObject);
          break;
        case 2001:
          state.power.push(jsonObject);
          break;
        default:
          console.warn(`Unknown message type: ${type}`);
      }
    }
    else if (code === 1 && jsonObject) {
      // 实时点名的数据
      const { type } = message
      // console.log(message)
      if (type === 1) {
        // 点名的进度
        console.log(jsonObject)
        state.rollCall = { ...state.rollCall, ...jsonObject };
        store.dispatch('bigScreen/updateVariables',state.rollCall)
      } else if (type === 0 ) {
      //巡航的进度
        console.log(jsonObject)
        state.cruiseDate = { ...state.cruiseDate, ...jsonObject};
        store.dispatch('bigScreen/updateCruiseDate', state.cruiseDate)
      }
    }
    else if(code === 2 && jsonObject) {
      //异常
      const { type,items } = jsonObject;
      // console.log(jsonObject)
      let alertMessage = '';
      let alertType = 'error';

      switch (type) {
        case 2001:
          //网络异常
          if (items === 0) {
            alertMessage = '检测到机器狗离线，请检查机器狗是否开机或网络是否断开！';
            state.onlineState = 0
          } else if (items === 1) {
            state.onlineState = 1
            return;
          }
          break;
        case 2002:
          //长期不动
          // console.log("长期不动")
          if (items === 0) {
            state.nomoveTimes ++
            if (state.nomoveTimes > 15 && state.nomoveTimes < 30) {
              alertMessage = '检测到机器狗已经30s不动，请检查是否遇到障碍或地图飘了!';
            } else if (state.nomoveTimes > 30)  {
              alertMessage = '检测到机器狗已经60s不动，请检查是否遇到障碍或地图飘了!';
            } else {
              return;
            }
          } else if (items ===1) {
            state.nomoveTimes = 0
            return;
          }
          break;
        case 2003:
        //  开始导航时，狗不在充电桩上
          alertMessage = '准备开始执行任务...检测到机器狗不在充电桩上！请手动将机器狗遥控回到充电桩上再开始执行任务。';
          break;
        case 2004:
          //下发导航任务的时候上一个导航任务还没有结束
          alertMessage = '当前正在执行导航任务，不能开启新的导航任务！请等待机器狗完成本轮任务再执行任务';
          break;
        case 2005:
        //  执行本次导航任务时判断电量还够不够
          alertMessage = '当前机器狗电量不足20%，不能执行任务！请确保电量充足后执行任务。';
          break;
        default:
          console.warn(`Unknown message type: ${type}`);
      }
      // 触发全局警告
      store.dispatch('alert/showAlert', {
        message: alertMessage,
        type: alertType
      });
    }
    else if (code === 3 && jsonObject){
      console.log(jsonObject)
    //  点名监控
      state.personRollCall.push(jsonObject)
    }
    else if (code === 4 && jsonObject) {
      //异常监控
      console.log(jsonObject)
      state.statistics = {severity:jsonObject.serverCounts,normal:jsonObject.normCounts};
      store.dispatch('bigScreen/updateStatistics', state.statistics)

      state.firePersonError.push(jsonObject)
      console.log("更新后的 firePersonError:", state.firePersonError);
    }
    else {
      console.warn(`Unknown message source: ${code}`);
    }
  },
  setConnected(state,status) {
    state.isConnected = status;
  },
  // incrementReconnectAttempts(state) {
  //   state.reconnectAttempts++;
  // },
  // resetReconnectAttempts(state) {
  //   state.reconnectAttempts = 0;
  // },
};

// 定义 actions 以便于进行异步操作
const actions = {
  // 初始化 WebSocket 连接
  initWebSocket({ commit,state,dispatch }) {
    if (state.websocket) {
      state.websocket.close();//关闭已有的 WebSocket 实例，防止重复连接
    }
    const websocket = new WebSocket('ws://192.168.43.118:8080/websocket');

    // 打开 websocket
    websocket.onopen = () => {
      console.log("WebSocket 连接成功！");
      commit('setConnected', true);
      commit('resetReconnectAttempts');
    }

    // 监听 WebSocket 的消息事件
    websocket.onmessage = (event) => {
      if (typeof event.data === 'string') {
        const message = JSON.parse(event.data);
        commit('addMessage', message); // 将消息添加到状态中
      } else if (typeof event.data === 'object') {
        // console.log(event.data)
        commit('addMessage', event.data);
      }
    };

    // 关闭 websocket
    websocket.onclose = () => {
      console.log("WebSocket 已关闭！");
      commit('setConnected', false);
      store.dispatch('alert/showAlert', {
        message: "网络断开！请重启后端后刷新重连！",
        type: 'error'
      });
      // if (state.reconnectAttempts < state.maxReconnectAttempts) {
      //   commit('incrementReconnectAttempts');
      //   setTimeout(() => {
      //     console.log(`正在尝试重连第 ${state.reconnectAttempts} 次...`);
      //     dispatch('initWebSocket');
      //   }, 2000); // 重连间隔 2 秒
      // } else {
      //   console.error("WebSocket 重连失败！");
      //   dispatch('showConnectionError'); // 弹出 WebSocket 连接失败提示
      // }
    };

    // websocket 出错
    websocket.onerror = (error) => {
      console.error("WebSocket 出现错误：", error);
      commit('setConnected', false);
      websocket.close();
    };

    // 将 WebSocket 实例保存到状态中
    commit('setWebSocket', websocket);
  },
  showConnectionError() {
    alert("WebSocket 连接失败，请检查网络或服务器！");
  },
  sendMessage({ state }, message) {
    if (state.websocket && state.websocket.readyState === WebSocket.OPEN) {
      state.websocket.send(message);
    } else {
      console.error("WebSocket is not connected.");
    }
  },
};

// 定义 getters 以便于从状态中获取数据
const getters = {
  // 获取所有接收到的消息
  getBasic: (state) => state.basic,
  getIssueTask: (state) => state.issue_task,
  getCancelTask: (state) => state.cancel_task,
  getSearchTask: (state) => state.search_task,
  getMotionTask: (state) => state.motion_control,
  getPowerNum: (state) => state.power,
  getVideoBlob: (state) => state.videoBlob,
  getRollCall: (state) => state.rollCall,
  getPersonRollCall: (state) => state.personRollCall,
  getFirePersonError: (state) => state.firePersonError,
};

// 导出 WebSocket 模块
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
